package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.persistence.criteria.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.dao.SyslogininforDAO;
import com.ruoyi.system.domain.SysLogininfor;
import com.ruoyi.system.service.ISysLogininforService;

/**
 * 系统访问日志情况信息 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysLogininforServiceImpl implements ISysLogininforService
{

    @Autowired
    private SyslogininforDAO logininforDAO;

    /**
     * 新增系统登录日志
     * 
     * @param logininfor 访问日志对象
     */
    @Transactional
    @Override
    public void insertLogininfor(SysLogininfor logininfor)
    {
        logininforDAO.save(logininfor);
    }

    /**
     * 查询系统登录日志集合
     * 
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public Page<SysLogininfor> selectLogininforList(SysLogininfor logininfor,Pageable pageinfo)
    {
		return logininforDAO.findAll(getSpecification(logininfor),pageinfo);
    }
    
	@Override
	public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor) {
		return logininforDAO.findAll(getSpecification(logininfor));
	}
	
	private Specification<SysLogininfor> getSpecification(SysLogininfor logininfor){
		return (root, criteriaQuery, criteriaBuilder) -> {
			List<Predicate> predicateList = new ArrayList<>();
			if (!StringUtils.isEmpty(logininfor.getIpaddr())) {
				predicateList.add(criteriaBuilder.like(root.get("ipaddr"), "%"+logininfor.getIpaddr()+"%"));
			}
			if (!StringUtils.isEmpty(logininfor.getStatus())) {
				predicateList.add(criteriaBuilder.equal(root.get("status"), logininfor.getStatus()));
			}
			if (!StringUtils.isEmpty(logininfor.getUserName())) {
				predicateList.add(criteriaBuilder.equal(root.get("userName"), "%"+logininfor.getUserName()+"%"));
			}
			if (!StringUtils.isNull(logininfor.getParams().get("beginTime"))) {
				predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"), logininfor.getParams().get("beginTime").toString()));
			}
			
			if (!StringUtils.isNull(logininfor.getParams().get("endTime"))) {
				predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime"), logininfor.getParams().get("endTime").toString()));
			}
			
			Predicate[] p = new Predicate[predicateList.size()];
			return criteriaBuilder.and(predicateList.toArray(p));
		};
	}

    /**
     * 批量删除系统登录日志
     * 
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLogininforByIds(Long[] infoIds)
    {
    	logininforDAO.deleteAllByIdInBatch(Arrays.asList(infoIds));
        return 1;
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor()
    {
        logininforDAO.deleteAll();
    }

}
